/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2013
 */


package com.smg.community.controller;


import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseRestSpringController;
import javacommon.util.FileUtil;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import cn.org.rapid_framework.page.Page;

import com.smg.community.model.City;
import com.smg.community.model.CrmUser;
import com.smg.community.model.ExtJsPageData;
import com.smg.community.model.SmgIndexRightbottomImage;
import com.smg.community.model.SmgIndexTopFiveimages;
import com.smg.community.service.CityManager;
import com.smg.community.setting.ProjectSetting;
import com.smg.community.vo.query.SmgIndexRightbottomImageQuery;
import com.smg.community.vo.query.SmgIndexTopFiveimagesQuery;

/**
 * @author badqiu email:badqiu(a)gmail.com
 * @version 1.0
 * @since 1.0
 */

@Controller
@RequestMapping("/smgindexrightbottomimage")
@SuppressWarnings("unchecked")
public class SmgIndexRightbottomImageController extends BaseRestSpringController<SmgIndexRightbottomImage,java.lang.Integer>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	//默认多列排序,example: username desc,createTime asc
	protected static final String DEFAULT_SORT_COLUMNS = null; 
	
	@RequestMapping(value="/list")
	public String  list(HttpServletRequest request,ModelMap model){
		CrmUser loginUser = getLoginUser(request);
		String userCityNos = loginUser.getCityNo();
		if  ( userCityNos == null ||  userCityNos.length() == 0 ){
			model.addAttribute("message","用户不拥有任何城市");
			return "/common/error";
		}
		List<City> cityList = CityManager.getCityListByUserCityNos(userCityNos);
		model.addAttribute("cityList",cityList);
		userCityNos = userCityNos.substring(0);
		userCityNos = userCityNos.substring(1,userCityNos.length()-1);
		model.addAttribute("userCityNos",userCityNos);
		return "/index/listrightbottomimages";
	}
	
	@RequestMapping(value="/getpagedata")
	public    @ResponseBody ExtJsPageData<SmgIndexRightbottomImage> getListData(SmgIndexRightbottomImageQuery query){
		Page<SmgIndexRightbottomImage> page   = smgIndexRightbottomImageManager.findPage(query);
		return transToExtJsPageData(page.getResult(),page.getTotalCount());
	}

	@RequestMapping(value="/update")
	public @ResponseBody Map<String,String> update(HttpServletRequest request,SmgIndexRightbottomImage smgIndexTopFiveimages){
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		try {
			multipartRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace(); 
		}
		CommonsMultipartFile file = (CommonsMultipartFile) multipartRequest.getFile("filename");
    	if(file.getSize()/1024/1024 >= Integer.parseInt(request.getParameter("maxSize"))){
			return writeErrorMessage("文件过大");
		}
    	String fileName = new java.text.SimpleDateFormat("HHmmssSSS").format(System.currentTimeMillis());
    	String path = FileUtil.uploads(multipartRequest,com.smg.community.setting.ProjectSetting.getPathByDate(request),fileName,"filename");
		try {
			if ( path != null){
				path = path.substring(path.indexOf("crm") );
				path  = path.toLowerCase();
				smgIndexTopFiveimages.setImagepath(ProjectSetting.IMAGE_URL_PREFIX+path);
			}
			smgIndexRightbottomImageManager.update(smgIndexTopFiveimages);
		}catch(Exception e){
			e.printStackTrace();
			return writeErrorMessage();
		}
		return writeSuccessMessage();
	}
	@RequestMapping(value="/copy/{showOrder}")
	public @ResponseBody Map<String,String> copy(@PathVariable int showOrder,HttpServletRequest request,SmgIndexTopFiveimages smgIndexTopFiveimages){
		try {
			smgIndexRightbottomImageManager.copy(showOrder);
		}catch(Exception e){
			e.printStackTrace();
			return writeErrorMessage();
		}
		return writeSuccessMessage();
	}
	@RequestMapping(value="/setonline/{id}")
	public @ResponseBody Map<String,String> setOnline(@PathVariable int id,HttpServletRequest request,SmgIndexTopFiveimages smgIndexTopFiveimages){
		try {
			smgIndexRightbottomImageManager.setOnline(id);
		}catch(Exception e){
			e.printStackTrace();
			return writeErrorMessage();
		}
		return writeSuccessMessage();
	}
	
}

